/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.core.util;

import java.util.HashMap;
import java.util.Map;

public class PrimitiveUtil {
    private static final Map DEFAULT_INSTANCES = new HashMap();

    static {
        DEFAULT_INSTANCES.put(boolean.class, Boolean.FALSE);
        DEFAULT_INSTANCES.put(byte.class, new Byte((byte) 0));
        DEFAULT_INSTANCES.put(long.class, new Long(0));
        DEFAULT_INSTANCES.put(short.class, new Short((short) 0));
        DEFAULT_INSTANCES.put(float.class, new Float(0));
        DEFAULT_INSTANCES.put(double.class, new Double(0));
        DEFAULT_INSTANCES.put(int.class, new Integer(0));
    }

    public static Object getDefaultInstance(Class primitiveClass) {
        if (!primitiveClass.isPrimitive()) {
            throw new IllegalArgumentException("Class " + primitiveClass.getName() + " is not primitive");
        }
        return DEFAULT_INSTANCES.get(primitiveClass);
    }

    public static Class getBoxedClass(Class primitiveClass) {
        return getDefaultInstance(primitiveClass).getClass();
    }
}
